% Copyright (C) 2014-19 Benjamin Born and Johannes Pfeifer 
%
% This is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This code is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% For a copy of the licencse,
% see <http://www.gnu.org/licenses/>.

clear all
addpath('../Auxiliary_Files/')
if ~isfolder('Results')
    mkdir('.','Results');
end
load('../data_array_for_regression_stacked_by_variable_CDS_new');

%% prepare Tables
generate_table_headers;

%% do figures and descriptives
close all
n_countries=length(country_header);
suffix='';
end_date_descriptives=2017.75;
sample_restriction_end_quarter= [pos.spread_end_quarter pos.spread_end_quarter_lag_1];
basic_timeline=squeeze(data_array_for_regression_stacked_by_variable(:,1,pos.timeline));
    
Indicator_correlation=NaN(n_countries,1);
Indicator_correlation_demeaned=NaN(n_countries,1);
FE_correlation=NaN(n_countries,2);
Fiscal_correlation=NaN(n_countries,4);
CDS_correlation=NaN(n_countries,6);
fid_CDS_corr=fopen('Results/correlation_CDS_horizons.txt','w+');
Recession_average=NaN(n_countries,1);
Fiscal_stress_average=NaN(n_countries,1);
Smoothed_Indicator_average=NaN(n_countries,1);
CDS_spread_corr=NaN(n_countries,1);
Ifo_cycle_corr=NaN(n_countries,1);
g_descriptives=NaN(n_countries,6);
fiscal_descriptives=NaN(n_countries,2);
cons_inv_NX_descriptives=NaN(n_countries,2);
spread_descriptives_end_quarter=NaN(n_countries,6);
peg_share=NaN(n_countries,1);
corr_d_y_spread=NaN(n_countries,1);
corr_d_g_spread=NaN(n_countries,1);
indic_fig_handle=figure('Name',['Indicators_1',suffix]);
set(indic_fig_handle,'PaperType','a4','PaperPositionMode','manual','PaperUnits','centimeters','PaperPosition',[1,1,19,27],'renderer', 'painters','units','normalized','outerposition',[0 0 1 1]);
indic_fig_iter=1;
indic_fig_subplot_iter=1;

CDS_fig_handle=figure('Name',['CDS vs. Spreads_1',suffix]);
set(CDS_fig_handle,'PaperType','a4','PaperPositionMode','manual','PaperUnits','centimeters','PaperPosition',[1,1,19,27],'renderer', 'painters','units','normalized','outerposition',[0 0 1 1]);
CDS_fig_iter=1;
CDS_fig_subplot_iter=1;

%% prepare building matrix for running panel regression

for country_iter=1:length(country_header) %iterate over alphabetically sorted countries
    country_identifier=strmatch(country_header{country_iter},country_indicator_names_mapping,'exact');
    descriptives_country_temp_matrix=squeeze(data_array_for_regression_stacked_by_variable(:,data_array_for_regression_stacked_by_variable(1,:,pos.country_ident)==country_identifier,:));
    if ~isempty(descriptives_country_temp_matrix)            
            %cut sample to jointly available sample
            descriptives_country_temp_matrix(descriptives_country_temp_matrix(:,pos.timeline)>end_date_descriptives | any(isnan(descriptives_country_temp_matrix(:,sample_restriction_end_quarter)),2),3:end)=NaN;
            if size(descriptives_country_temp_matrix,1)~=length(basic_timeline)
                error('Temporary country matrix has wrong length')
            end
            
            %get share of float episodes
            peg_share(country_iter,1)=1-nanmean(descriptives_country_temp_matrix(:,pos.float_episode));
            
            g_y=descriptives_country_temp_matrix(:,pos.g_y_ratio);
            g_descriptives(country_iter,1)=basic_timeline(find(~any(isnan(descriptives_country_temp_matrix(:,sample_restriction_end_quarter)),2),1,'first'));
            g_descriptives(country_iter,2)=basic_timeline(find(~any(isnan(descriptives_country_temp_matrix(:,sample_restriction_end_quarter)),2),1,'last'));
            g_descriptives(country_iter,3:end)=[min(g_y) max(g_y) nanmean(g_y) nanstd(g_y)];
            
            fprintf(fid_g,'%-30s \t & \t %6s \t & \t %6s \t & \t %2.1f \t & \t %2.1f \t & \t %2.1f \t & \t %2.1f \\\\ \n',country_header{1,country_iter},[num2str(floor(g_descriptives(country_iter,1))),'Q',num2str(mod(g_descriptives(country_iter,1),1)/0.25+1)],[num2str(floor(g_descriptives(country_iter,2))),'Q',num2str(mod(g_descriptives(country_iter,2),1)/0.25+1)],g_descriptives(country_iter,3:end)*100);
            clear g_y;
                        
            %% Compute Recession share
            Fz_output_data=descriptives_country_temp_matrix(:,pos.empirical_cdf_output_gap);
            
            Recession_average(country_iter,1)=nanmean(Fz_output_data);
            clear Fz_output_data;
            
            Fz_stress_data=descriptives_country_temp_matrix(~any(isnan(descriptives_country_temp_matrix(:,[pos.Indicator_recession_AG, pos.empirical_cdf_country_group_specific_end_quarter])),2),pos.empirical_cdf_country_group_specific_end_quarter);
            Fiscal_stress_average(country_iter,1)=nanmean(Fz_stress_data);
            
            %% Cyclical properties
            corr_d_y_spread(country_iter,1)=nancorr(descriptives_country_temp_matrix(:,pos.spread_end_quarter),descriptives_country_temp_matrix(:,pos.y_growth));
            corr_d_g_spread(country_iter,1)=nancorr(descriptives_country_temp_matrix(:,pos.spread_end_quarter),descriptives_country_temp_matrix(:,pos.g_growth));
                    
            temp_indices=[pos.CDS_data_end_quarter_maturity_5 pos.CDS_data_end_quarter_maturity_1 pos.CDS_data_end_quarter_maturity_2 pos.CDS_data_end_quarter_maturity_3 pos.CDS_data_end_quarter_maturity_4 pos.CDS_data_end_quarter_maturity_7 pos.CDS_data_end_quarter_maturity_10];
            temp_mat_corr_CDS=descriptives_country_temp_matrix(~any(isnan(descriptives_country_temp_matrix(:,temp_indices)),2),temp_indices);
            if ~isempty(temp_mat_corr_CDS)
               temp_mat_corr_CDS=corr(temp_mat_corr_CDS);
               CDS_correlation(country_iter,:)=temp_mat_corr_CDS(1,2:end);
               fprintf(fid_CDS_corr,'%-30s: \t %4.3f \t %4.3f \t %4.3f \t %4.3f \t %4.3f \t %4.3f\n',country_header{1,country_iter},CDS_correlation(country_iter,:));                 
            end
            clear temp_indices temp_mat_corr_CDS
            
            %% Descriptives CDS vs. spreads
            temp_mat_CDS_spreads=descriptives_country_temp_matrix(~any(isnan(descriptives_country_temp_matrix(:,[pos.empirical_cdf_country_group_specific_end_quarter, pos.spread_end_quarter_without_CDS, pos.CDS_end_quarter ])),2),[pos.timeline pos.spread_end_quarter_without_CDS pos.CDS_end_quarter]);
            if ~isempty(temp_mat_CDS_spreads)
                CDS_spread_corr(country_iter)=corr(temp_mat_CDS_spreads(:,2),temp_mat_CDS_spreads(:,3));
                fprintf('<CDS: %-30s \t %4.3f\n',country_header{1,country_iter},CDS_spread_corr(country_iter))
                
                figure(CDS_fig_handle)
                subplot(6,3,CDS_fig_subplot_iter);
                hp1=plot(temp_mat_CDS_spreads(:,1),temp_mat_CDS_spreads(:,2),'b',temp_mat_CDS_spreads(:,1),temp_mat_CDS_spreads(:,3),'r--','LineWidth',1);
                xlim([2003.5 2014.75])
                set(gca,'XTickMode','manual')
                set(gca,'XTick',[2004:2:2014])
                x_tick_temp=get(gca,'XTicklabel');
                for label_iter=1:size(x_tick_temp,1)
                    x_tick_temp{label_iter,1}=x_tick_temp{label_iter,1}(:,3:end);
                end
                set(gca,'XTicklabel',x_tick_temp)
                data_temp=temp_mat_CDS_spreads(temp_mat_CDS_spreads(:,1)>=2003 & temp_mat_CDS_spreads(:,1)<=2014.75,2:end);
                ylim([-0.2 1.05*max(data_temp(:))])
                title(country_header{country_iter},'FontSize',10)
                if mod(CDS_fig_subplot_iter,17)==0
                    legend(hp1,'Constructed Spread','CDS','Location','NorthWest')
                    print(['Results/CDS_vs_spread_per_country_',num2str(CDS_fig_iter),suffix],'-depsc2')
                    CDS_fig_iter=CDS_fig_iter+1;
                    CDS_fig_handle=figure('Name',['CDS vs. Spreads_',num2str(CDS_fig_iter),suffix]);
                    set(CDS_fig_handle,'PaperType','a4','PaperPositionMode','manual','PaperUnits','centimeters','PaperPosition',[1,1,19,27],'renderer', 'painters','units','normalized','outerposition',[0 0 1 1]);
                    CDS_fig_subplot_iter=0;
                end
                CDS_fig_subplot_iter=CDS_fig_subplot_iter+1;
            else
                CDS_spread_corr(country_iter,1)=NaN;
            end

            %% Descriptives Spreads
            %get dates
            spread_descriptives_end_quarter(country_iter,1)=basic_timeline(find(~isnan(descriptives_country_temp_matrix(:,pos.spread_end_quarter)),1,'first'));
            spread_descriptives_end_quarter(country_iter,2)=basic_timeline(find(~isnan(descriptives_country_temp_matrix(:,pos.spread_end_quarter)),1,'last'));
            %get descriptives
            spread_descriptives_end_quarter(country_iter,3:end)=[min(descriptives_country_temp_matrix(:,pos.spread_end_quarter)) max(descriptives_country_temp_matrix(:,pos.spread_end_quarter)) nanmean(descriptives_country_temp_matrix(:,pos.spread_end_quarter)) nanstd(descriptives_country_temp_matrix(:,pos.spread_end_quarter))];
            
            if descriptives_country_temp_matrix(find(~isnan(descriptives_country_temp_matrix(:,pos.developed_country)),1,'first'),pos.developed_country)==1
                fprintf(fid_spread,'%-30s \t & \t D \t & \t %3.2f  \t & \t %3.2f  \t & \t %3.2f \t & \t %3.2f  \t & \t %3.2f\t & \t %3.2f \t\\\\ \n',country_header{1,country_iter},spread_descriptives_end_quarter(country_iter,3:end),corr_d_y_spread(country_iter,1),corr_d_g_spread(country_iter,1));
            else
                fprintf(fid_spread,'%-30s \t & \t E \t & \t %3.2f  \t & \t %3.2f  \t & \t %3.2f \t & \t %3.2f  \t & \t %3.2f\t & \t %3.2f \t\\\\ \n',country_header{1,country_iter},spread_descriptives_end_quarter(country_iter,3:end),corr_d_y_spread(country_iter,1),corr_d_g_spread(country_iter,1));
            end
            %                 fprintf(fid_spread,'%-30s \t & \t %6s \t & \t %6s \t & \t %3.2f \t & \t %3.2f \t & \t %3.2f \t & \t %3.2f  \t & \t %3.2f\t & \t %3.2f \t\\\\ \n',country_header{1,country_iter},[num2str(floor(spread_descriptives_end_quarter(country_iter,1))),'Q',num2str(mod(spread_descriptives_end_quarter(country_iter,1),1)/0.25+1)],[num2str(floor(spread_descriptives_end_quarter(country_iter,2))),'Q',num2str(mod(spread_descriptives_end_quarter(country_iter,2),1)/0.25+1)],spread_descriptives_end_quarter(country_iter,3:end),corr_d_y_spread(country_iter,1),corr_d_g_spread(country_iter,1));
                  
            %% Plot Indicator Figure
            figure(indic_fig_handle);
            subplot(6,3,indic_fig_subplot_iter);
            plot(basic_timeline,descriptives_country_temp_matrix(:,pos.empirical_cdf_country_group_specific_end_quarter),'-',...
                basic_timeline,descriptives_country_temp_matrix(:,pos.empirical_cdf_output_gap),'r--','LineWidth',1)
            axis tight
            first_obs=find(~all(isnan(descriptives_country_temp_matrix(:,[pos.empirical_cdf_country_group_specific_end_quarter,pos.empirical_cdf_output_gap])),2),1,'first');
            last_obs=find(~all(isnan(descriptives_country_temp_matrix(:,[pos.empirical_cdf_country_group_specific_end_quarter,pos.empirical_cdf_output_gap])),2),1,'last');
            
            xlim([basic_timeline(first_obs) basic_timeline(last_obs)])
            if basic_timeline(last_obs)-basic_timeline(first_obs)>10
                step_size=3;
            else
                step_size=2;
            end
            set(gca,'XTickMode','manual')
            set(gca,'XTick',[ceil(basic_timeline(first_obs)):step_size:basic_timeline(last_obs)]')
            x_tick_temp=get(gca,'XTickLabel');
            for label_iter=1:size(x_tick_temp,1)
                x_tick_temp{label_iter,1}=x_tick_temp{label_iter,1}(:,3:end);
            end
            set(gca,'XTickLabel',x_tick_temp)
            title(country_header{1,country_iter})
            
            if mod(indic_fig_subplot_iter,17)==0
                legend('Fiscal Stress','Recession','Location','NorthWest')
                %                 set(gca,'FontSize',fontsize)
                print(['Results/Indicators_per_country_',num2str(indic_fig_iter),suffix],'-depsc2')
                indic_fig_iter=indic_fig_iter+1;
                indic_fig_handle=figure('Name',['Indicators_',num2str(indic_fig_iter),suffix]);
                set(indic_fig_handle,'PaperType','a4','PaperPositionMode','manual','PaperUnits','centimeters','PaperPosition',[1,1,19,27],'renderer', 'painters','units','normalized','outerposition',[0 0 1 1]);
                indic_fig_subplot_iter=0;
            end
            indic_fig_subplot_iter=indic_fig_subplot_iter+1;
        else
            error(['Not enough data for ' country_header{1,country_iter},'. Please add the country to delete_countries.'])
        end
    if country_iter==n_countries
        figure(CDS_fig_handle);
        legend(hp1,'Constructed Spread','CDS','Location','NorthWest')
        print(['Results/CDS_vs_spread_per_country_',num2str(CDS_fig_iter),suffix],'-depsc2')
        figure(indic_fig_handle);
        legend('Fiscal Stress','Recession','Location','NorthWest')
        print(['Results/Indicators_per_country_',num2str(indic_fig_iter),suffix],'-depsc2')
    end
    if ~isempty(find(strcmp(country_header{1,country_iter},'Italy')))
        figure('Name',['Fiscal Stress and Recessions in Italy',suffix]);
        plot(basic_timeline,descriptives_country_temp_matrix(:,pos.empirical_cdf_country_group_specific_end_quarter),'-',...
            basic_timeline,descriptives_country_temp_matrix(:,pos.Indicator_recession_AG),'k--','LineWidth',1)
        axis tight
        first_obs=find(~any(isnan(descriptives_country_temp_matrix(:,[pos.empirical_cdf_country_group_specific_end_quarter,pos.Indicator_fiscal_stress_demeaned_end_quarter,pos.Indicator_recession_AG])),2),1,'first');
        last_obs=find(~any(isnan(descriptives_country_temp_matrix(:,[pos.empirical_cdf_country_group_specific_end_quarter,pos.Indicator_fiscal_stress_demeaned_end_quarter,pos.Indicator_recession_AG])),2),1,'last');
        
        xlim([basic_timeline(first_obs) basic_timeline(last_obs)])
        legend('Fiscal Stress','Recession','Location','North')
        print(['Results/Indicators_Italy',suffix],'-depsc2')
    end
    clear first_obs last_obs
end

generate_table_footers;
